package com.gykfbxxh.imovie.view.set;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.app.utils.util.AppTools;
import com.app.utils.util.ToastUtils;
import com.app.utils.util.file.AppFileUtils;
import com.gykfbxxh.imovie.R;
import com.gykfbxxh.imovie.base.BaseActivity;
import com.gykfbxxh.imovie.config.AppConfigTools;
import com.gykfbxxh.imovie.config.ConfigKey;
import com.gykfbxxh.imovie.config.URIContant;
import com.gykfbxxh.imovie.custom.net.ParamMap;
import com.gykfbxxh.imovie.greendao.GDManager;
import com.gykfbxxh.imovie.manager.AppConfigManager;
import com.gykfbxxh.imovie.manager.UserInforManager;
import com.gykfbxxh.imovie.utils.DialogUtils;
import com.gykfbxxh.imovie.utils.HttpUtils;
import com.gykfbxxh.imovie.view.user.activity.DisclaimerActivity;
import com.gykfbxxh.imovie.view.user.activity.UserInfoActivity;

import java.io.File;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;

public class SetActivity extends BaseActivity implements View.OnClickListener {
    private RelativeLayout rl_userinfoview,rl_clearcache,rl_shuoming;
    private TextView tv_cancel ,tv_version ,tv_cachesize;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set);
        setTitle("设置");
        initView();
        initData();
    }

    private void initData() {
        tv_version.setText(AppTools.getVersionName());
        // 加载缓存大小
        new Thread() {
            @Override
            public void run() {
                double cacheSize = 0;
                try {
                    cacheSize = AppFileUtils.getFolderSize(new File(AppConfigTools.SDCARDPATH1));
                    cacheSize += AppFileUtils.getFolderSize(new File(AppConfigTools.SDCARDPATH));
                } catch (Exception e) {
                    e.printStackTrace();
                }
                //Double值取得小数点后两位方法
                final double cacheSize2 = new BigDecimal((cacheSize) / 1024 / 1024 / 2).setScale(2, RoundingMode.UP).doubleValue();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        tv_cachesize.setText(cacheSize2 + "MB");
                    }
                });

            }
        }.start();

    }

    private void initView() {
        rl_userinfoview = (RelativeLayout)findViewById(R.id.rl_userinfoview);
        rl_clearcache = (RelativeLayout)findViewById(R.id.rl_clearcache);
        rl_shuoming = (RelativeLayout)findViewById(R.id.rl_shuoming);
        tv_cancel = (TextView)findViewById(R.id.tv_cancel);
        tv_version = (TextView)findViewById(R.id.tv_version);
        tv_cachesize = (TextView)findViewById(R.id.tv_cachesize);
        rl_userinfoview.setOnClickListener(this);
        rl_clearcache.setOnClickListener(this);
        rl_shuoming.setOnClickListener(this);
        tv_cancel.setOnClickListener(this);
        if(UserInforManager.getInstance().isLogin()) {
            tv_cancel.setVisibility(View.VISIBLE);
        }

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.rl_userinfoview:
                if(UserInforManager.getInstance().isLogin()) {
                    Intent intent = new Intent(mActivity, UserInfoActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.rl_clearcache:
                cleatCache();
                break;
            case R.id.rl_shuoming:
                Intent intent = new Intent(this, DisclaimerActivity.class);
                startActivity(intent);
                break;
            case R.id.tv_cancel:
                cancelLogin();
                tv_cancel.setVisibility(View.GONE);
                break;
        }
    }

    // 清空缓存
    private void cleatCache() {
        if ("0.0MB".equals(tv_cachesize.getText())) {
            ToastUtils.makeText("当前没有缓存");
            return;
        }

        HashMap<String, String> style = new HashMap<>();
        style.put("title", mActivity.getResources().getString(R.string.common_prompt));
        style.put("desc", this.getString(R.string.set_clear_cache_sure));
        style.put("btnPosi", this.getString(R.string.common_confirm));
        style.put("btnNega", this.getString(R.string.common_cancel));

        DialogUtils.getInstance().setOnDialogBtnClick(new DialogUtils.OnDialogBtnClickListener() {
            @Override
            public void onPositiveClick() {
                DialogUtils.getInstance().dismissCustomDialog();
                clearCachOnThread();
            }

            @Override
            public void onNegativeClick() {
                DialogUtils.getInstance().dismissCustomDialog();
            }
        });
        DialogUtils.getInstance().showCustomDialog(this, style);

    }

    private void clearCachOnThread() {
        final Handler handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                tv_cachesize.setText("0.0MB");
                ToastUtils.makeText(getString(R.string.set_clear_cache_suces));
            }
        };
        new Thread() {
            public void run() {
                AppFileUtils.clearCache(new File(AppConfigTools.SDCARDPATH1));// 清除所有缓存图片
                AppFileUtils.clearCache(new File(AppConfigTools.SDCARDPATH));// 清除所有缓存图片
                handler.sendEmptyMessage(0);
            }

            ;
        }.start();
    }


    private void cancelLogin() {
        if (UserInforManager.getInstance().isLogin()) {
            UserInforManager.getInstance().setUserInfor(null);
            GDManager.getInstance().delUserInfoGD();
            ParamMap paramMap = new ParamMap();
            HttpUtils.addPostResultRequest(URIContant.LOGINOUT,  mPageName, paramMap, null);
            AppConfigTools.TOKENSTR = "" ;
            SharedPreferences.Editor editor = AppConfigManager.getInstance().getEditor();
            editor.putString(ConfigKey.user_token,"");
            editor.commit();
            ToastUtils.makeText("退出登录成功");
            onBackPressed();
        }
    }

    @Override
    public void onBackPressed() {
        Intent intent = new Intent();
        setResult(RESULT_OK, intent);
        super.onBackPressed();
    }

    @Override
    public void doBack(View view) {
        Intent intent = new Intent();
        setResult(RESULT_OK, intent);
        super.doBack(view);
    }
}
